package ig.metrica.ui;

//import ig.metrica.Modelo;
import ig.metrica.ModelosDTO;
import ig.metrica.ui.modelo.WizzardModelo;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;

public abstract class PantallaBusquedaSevinproPanel extends JPanel {
    
    private WizzardSevinproPanel wizzard;
    private Object[] llaveVO;
    
    private JButton btnBuscar;
    private JButton btnSeleccionar;
    private JButton btnSalir;
    //public JlEstadoModeo dmodelo;
    protected JTable tabla;
    protected  Collection listaFull = null;
    Integer idModelo=-1;
    /**
     *
     * @param panelMedio JPanel con la definicion grafica de los campos para los que se permite realizar consultas
     * @param wizzard Componente que activo la pantalla de busqueda.
     * @param modelotabla
     */
    public PantallaBusquedaSevinproPanel(JPanel panelMedio, WizzardSevinproPanel wizzard, ModeloTablaBusquedaSevinpro modelotabla){
        
        this.wizzard = wizzard;
        String path = "./lib/images/";
        JToolBar panelSuperior;
        JPanel panelEscalable;
        JPanel panelInferior;
        
        setLayout( new BorderLayout() );
        
        add( panelSuperior = new JToolBar(), BorderLayout.NORTH );
        panelSuperior.setLayout( new FlowLayout( FlowLayout.LEFT ));
        panelSuperior.setEnabled(false);
                        /*panelSuperior.setBorder(BorderFactory.createCompoundBorder(
                                        BorderFactory.createEmptyBorder(8, 0, 0, 0), BorderFactory.createEtchedBorder()));*/
        
        
        panelSuperior.add( btnBuscar = new JButton( new javax.swing.ImageIcon(getClass().getResource("/ig/util/images/buscar.gif"))));
        panelSuperior.add( btnSeleccionar = new JButton( new javax.swing.ImageIcon(getClass().getResource("/ig/util/images/aceptar.gif"))));
        panelSuperior.add( btnSalir = new JButton( new javax.swing.ImageIcon(getClass().getResource("/ig/util/images/salir.gif"))));
//			panelSuperior.add( btnBuscar = new JButton( CargadorImagenes.crearIconoPath(path+"buscar.gif")) );
//			panelSuperior.add( btnSeleccionar = new JButton( CargadorImagenes.crearIconoPath(path+"aceptar.gif")) );
//			panelSuperior.add( btnSalir = new JButton( CargadorImagenes.crearIconoPath(path+"salir.gif")) );
        
        add( panelEscalable = new JPanel(), BorderLayout.CENTER );
        panelEscalable.setLayout( new GridBagLayout() );
        panelEscalable.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );
        //------------------------------------------------------------------
//			panelEscalable.add( panelMedio, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)  );
        panelEscalable.add( panelInferior = new JPanel(), new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)  );
        panelInferior.setLayout( new BorderLayout() );
        panelInferior.setBorder( BorderFactory.createEmptyBorder(30, 0, 0, 0) );
        panelInferior.add( new JScrollPane( tabla = new JTable( modelotabla )), BorderLayout.CENTER );
        
        //-----------------------------
        //btnBuscar.setContentAreaFilled( false );
        btnBuscar.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        btnBuscar.setOpaque(false);
        btnBuscar.setToolTipText("Buscar");
        btnBuscar.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){accionBuscar();}});
        //-----------------------------
        //btnSeleccionar.setContentAreaFilled( false );
        btnSeleccionar.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        btnSeleccionar.setOpaque(false);
        btnSeleccionar.setToolTipText("Seleccionar");
        btnSeleccionar.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){accionSeleccionar();}});
        //-----------------------------
        //btnSalir.setContentAreaFilled( false );
        btnSalir.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        btnSalir.setOpaque(false);
        btnSalir.setToolTipText("Salir");
        btnSalir.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){btnSalirActionPerformed(e);}});
    }
    
    protected abstract void accionBuscar();
    
    protected  void accionSeleccionar(){
        int fila = tabla.getSelectedRow();
        if (fila!=-1){
            try {
                ((JInternalFrame)this.getParent().getParent().getParent().getParent()).dispose();
            } catch (Exception e) {
                this.getParent().getParent().getParent().getParent().setVisible(false);
            }
            llaveVO = ((ModeloTablaBusquedaSevinpro)tabla.getModel()).getLLaveVO(fila);
            //wizzard.setLlave(llaveVO);
            int idModelo=-1;
            if(this.listaFull!=null){
                for (Object elem : listaFull) {
                    ModelosDTO modelo = (ModelosDTO)elem;
                    if(llaveVO[0].toString().trim().equals(modelo.getNombre().trim())){
                        ((WizzardModelo)wizzard).setIdModelo(modelo.getId());
                    }
                }
            }
            wizzard.setLlave(llaveVO);
        }else
            JOptionPane.showMessageDialog( this, "Debe escoger un registro de la tabla.","Mensaje",JOptionPane.OK_OPTION );
    }
    
    protected final void btnSalirActionPerformed(ActionEvent e){
        salir();
    }
    public void salir(){
        //wizzard.setLlave(null);
        //wizzard.setEstadoBotonera(true, false, false, true, false, false, true);
        try {
            ((JInternalFrame)this.getParent().getParent().getParent().getParent()).dispose();
        } catch (Exception ee) {
            this.getParent().getParent().getParent().getParent().setVisible(false);
        }
    }
    protected final void limpiarCriteriosBusqueda(){
        ((ModeloTablaBusquedaSevinpro)tabla.getModel()).setDatos( new ArrayList() );
    }
    
}
